﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using DTO;

namespace DATA
{
   public  class NewsREP
    {

        MODELGENERALDataContext context = new MODELGENERALDataContext();


        public List<NewsDTO> GetAllNews()
        {
            List<NewsDTO> newstb = new List<NewsDTO>();

            var newsv = from a in context.News
                        orderby a.newsDate descending
                        select a;
            foreach (var newstbv in newsv)
            {
                NewsType newsType = context.NewsTypes.Single(c => c.typeID == newstbv.typeID);
                NewsDTO dto = new NewsDTO();
                dto.CountView = newstbv.countView;
                dto.IsActived = newstbv.isActived;
                dto.NewsContent = newstbv.newsContent;
                dto.NewsDate = newstbv.newsDate;
                dto.NewsID = newstbv.newsID;
                dto.NewsSubContent = newstbv.newsSubContent;
                dto.NewsTitle = newstbv.newsTitle;
                dto.PanelID = newstbv.panelID;
                dto.TypeID = newsType.typeID;
                dto.TypeName = newsType.typeName;
                dto.NewsPicture = newstbv.newsPicture;
                dto.NewsPictureName = newstbv.newsPictureName;
                newstb.Add(dto);

            }
            return newstb;
        }

        public List<NewsDTO> GetActiveNewestNews()
        {
            List<NewsDTO> newstb = new List<NewsDTO>();

            var newsv = from a in context.News
                        where a.isActived == true
                        orderby a.newsDate descending
                        select a;
            foreach (var newstbv in newsv)
            {
                NewsType newsType = context.NewsTypes.Single(c => c.typeID == newstbv.typeID);
                NewsDTO dto = new NewsDTO();
                dto.CountView = newstbv.countView;
                dto.IsActived = newstbv.isActived;
                dto.NewsContent = newstbv.newsContent;
                dto.NewsDate = newstbv.newsDate;
                dto.NewsID = newstbv.newsID;
                dto.NewsSubContent = newstbv.newsSubContent;
                dto.NewsTitle = newstbv.newsTitle;
                dto.PanelID = newstbv.panelID;
                dto.TypeID = newsType.typeID;
                dto.TypeName = newsType.typeName;
                dto.NewsPicture = newstbv.newsPicture;
                dto.NewsPictureName = newstbv.newsPictureName;
                newstb.Add(dto);

            }
            return newstb;
        }

        public List<NewsDTO> GetActiveSubNews(int subPanelID)
        {
            List<NewsDTO> newstb = new List<NewsDTO>();

            var newsv = from a in context.News
                        where a.isActived ==true && a.panelID == subPanelID
                        orderby a.newsDate descending
                        select a;
            foreach (var newstbv in newsv)
            {
                NewsType newsType = context.NewsTypes.Single( c => c.typeID == newstbv.typeID);

                NewsDTO dto = new NewsDTO();
                dto.CountView = newstbv.countView;
                dto.IsActived = newstbv.isActived;
                dto.NewsContent = newstbv.newsContent;
                dto.NewsDate = newstbv.newsDate;
                dto.NewsID = newstbv.newsID;
                dto.NewsSubContent = newstbv.newsSubContent;
                dto.NewsTitle = newstbv.newsTitle;
                dto.PanelID = newstbv.panelID;
                dto.TypeID = newsType.typeID;
                dto.TypeName = newsType.typeName;
                dto.NewsPicture = newstbv.newsPicture;
                dto.NewsPictureName = newstbv.newsPictureName;
                newstb.Add(dto);

            }
            return newstb;
        }

        public List<NewsDTO> GetActiveNewsWithType(int tID)
        {
            List<NewsDTO> newstb = new List<NewsDTO>();

            var newsv = from a in context.News
                        where a.isActived == true && a.typeID == tID
                        orderby a.newsDate descending
                        select a;
            foreach (var newstbv in newsv)
            {
                NewsType newsType = context.NewsTypes.Single(c => c.typeID == newstbv.typeID);

                NewsDTO dto = new NewsDTO();
                dto.CountView = newstbv.countView;
                dto.IsActived = newstbv.isActived;
                dto.NewsContent = newstbv.newsContent;
                dto.NewsDate = newstbv.newsDate;
                dto.NewsID = newstbv.newsID;
                dto.NewsSubContent = newstbv.newsSubContent;
                dto.NewsTitle = newstbv.newsTitle;
                dto.PanelID = newstbv.panelID;
                dto.TypeID = newsType.typeID;
                dto.TypeName = newsType.typeName;
                dto.NewsPicture = newstbv.newsPicture;
                dto.NewsPictureName = newstbv.newsPictureName;
                newstb.Add(dto);

            }
            return newstb;
        }

        public List<NewsDTO> GetActiveNews(int panelID)
        {
            //lay cac sub panel thuoc panelID
            var sp = from s in context.Panels
                     where s.isActived == true && s.parentsID == panelID
                     select s.panelID;

            List<NewsDTO> newstb = new List<NewsDTO>();
            foreach (var spID in sp)
            {
                var newsv = from a in context.News
                            where a.isActived == true && a.panelID == spID
                            select a;
                foreach (var newstbv in newsv)
                {
                    NewsType newsType = context.NewsTypes.Single(c => c.typeID == newstbv.typeID);

                    NewsDTO dto = new NewsDTO();
                    dto.CountView = newstbv.countView;
                    dto.IsActived = newstbv.isActived;
                    dto.NewsContent = newstbv.newsContent;
                    dto.NewsDate = newstbv.newsDate;
                    dto.NewsID = newstbv.newsID;
                    dto.NewsSubContent = newstbv.newsSubContent;
                    dto.NewsTitle = newstbv.newsTitle;
                    dto.PanelID = newstbv.panelID;
                    dto.TypeID = newsType.typeID;
                    dto.TypeName = newsType.typeName;
                    dto.NewsPicture = newstbv.newsPicture;
                    dto.NewsPictureName = newstbv.newsPictureName;
                    newstb.Add(dto);

                }
            }
            var u = from e in newstb
                    orderby e.NewsDate descending
                    select e;

            return u.ToList();
        }

        public NewsDTO GetNews(int newsID)
        {
            New n = context.News.Single(w => w.newsID == newsID);
            NewsType t = context.NewsTypes.Single(y => y.typeID == n.typeID);

            NewsDTO dto = new NewsDTO();
            dto.CountView = n.countView;
            dto.IsActived = n.isActived;
            dto.NewsContent = n.newsContent;
            dto.NewsDate = n.newsDate;
            dto.NewsID = n.newsID;
            dto.NewsSubContent = n.newsSubContent;
            dto.NewsTitle = n.newsTitle;
            dto.PanelID = n.panelID;
            dto.TypeID = t.typeID;
            dto.TypeName = t.typeName;
            dto.NewsPicture = n.newsPicture;
            dto.NewsPictureName = n.newsPictureName;

            return dto;
        }

        public bool CountViewUp(int nID)
        {
            try
            {
                New acc = context.News.Single( nv => nv.newsID == nID );
                acc.countView = acc.countView + 1;
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool AddNew(NewsDTO newn)
        {
            New newnews = new New();
            try
            {
                newnews.countView = newn.CountView;
                newnews.isActived = newn.IsActived;
                newnews.newsContent = newn.NewsContent;
                newnews.newsDate = newn.NewsDate;
                newnews.newsSubContent = newn.NewsSubContent;
                newnews.newsTitle = newn.NewsTitle;
                newnews.panelID = newn.PanelID;
                newnews.typeID = newn.TypeID;
                newnews.newsPicture = newn.NewsPicture;
                newnews.newsPictureName = newn.NewsPictureName;
                context.News.InsertOnSubmit(newnews);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;

        }
        public void deleteNews(int newsID)
        {
            var acc = context.News.Single(a => a.newsID == newsID);
            context.News.DeleteOnSubmit(acc);
            context.SubmitChanges();


        }
        public bool UpdateNew(NewsDTO newn)
        {
            try
            {
                New newnews = context.News.Single(nv => nv.newsID == newn.NewsID);
                newnews.countView = newn.CountView;
                newnews.isActived = newn.IsActived;
                newnews.newsContent = newn.NewsContent;
                newnews.newsDate = newn.NewsDate;
                newnews.newsSubContent = newn.NewsSubContent;
                newnews.newsTitle = newn.NewsTitle;
                newnews.panelID = newn.PanelID;
                newnews.typeID = newn.TypeID;
                newnews.newsPicture = newn.NewsPicture;
                newnews.newsPictureName = newn.NewsPictureName;
                context.SubmitChanges();
            }

            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }

    }
}
